/**
 * MIT License
 * 
 * Copyright (c) 2024 - present @ ebraid
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#ifndef __EB_SOFTIRQ_H__
#define __EB_SOFTIRQ_H__


#include <list.h>


enum {
	HI_SOFTIRQ = 0,
	TIMER_SOFTIRQ,
	NET_TX_SOFTIRQ,
	NET_RX_SOFTIRQ,
	BLOCK_SOFTIRQ,
	IRQ_POLL_SOFTIRQ,
	TASKLET_SOFTIRQ,
	SCHED_SOFTIRQ,
	HRTIMER_SOFTIRQ,

	NR_SOFTIRQS
};


struct tasklet {
	list_node_t entry;
	void (*func)(unsigned long data);
	unsigned long data;
	unsigned long state;
};


/**
 * @brief register a softirq handler
 * @param index softirq index
 * @param action softirq handler
 * @return none
 */
void softirq_register(int index, void (*action)(void));


/**
 * @brief wakeup softirq
 * @param index softirq index
 * @return none
 */
void softirq_wakeup(int index);


#define DECLARE_TASKLET(name, _func, _data) \
	struct tasklet name = { \
		.func = _func, \
		.data = _data, \
		.state = 0,   \
	}


/**
 * @brief add a tasklet to the tasklet list
 * @param tasklet tasklet
 * @param func tasklet handler
 * @param data tasklet data
 * @return none
 */
void tasklet_init(struct tasklet *tasklet, void (*func)(unsigned long data), unsigned long data);


/**
 * @brief add a tasklet to the tasklet list, then wakeup the tasklet
 * @param tasklet tasklet
 * @return none
 */
void tasklet_schedule(struct tasklet *tasklet);


/**
 * @brief trigger tasklet action
 * @param tasklet tasklet
 * @param data parameter data
 * @return none
 */
void tasklet_action(struct tasklet *tasklet, unsigned long data);


/**
 * @brief kill a tasklet, it will remove the tasklet from the tasklet list
 * @param tasklet tasklet
 * @return none
 */
void tasklet_kill(struct tasklet *tasklet);


#endif // __EB_SOFTIRQ_H__
